"""
@Author：十
@Time：2024/10/11 14:58
@FileName：edit.py
@Description：各仪器的编辑页面
"""
import sys
from datetime import datetime

from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QWidget, QApplication

from UI.DCR import Ui_Form as DCR_Form
from UI.LR import Ui_Form as LR_Form
from UI.EC import Ui_Form as EC_Form


# 直流电阻测试仪
class DCR_EditWindow(QWidget, DCR_Form):
    Signal_Get_Date = pyqtSignal(object)

    def __init__(self, parent=None):
        super(DCR_EditWindow, self).__init__()

        self.parent = parent

        self.setupUi(self)

        self.Signal_Get_Date.connect(self.ShowData)

        self.pushButton.clicked.connect(self.SubmitEdit)

        self.edit_date = None

    def ShowData(self, data):
        try:
            self.lineEdit_Mode.setText(data['test_mode'])
            self.lineEdit_Electricity.setText(str(data['test_electricity']))
            self.lineEdit_A.setText(str(data['resistor_ao']))
            self.lineEdit_B.setText(str(data['resistor_bo']))
            self.lineEdit_C.setText(str(data['resistor_co']))
            self.edit_date = data
        except Exception as e:
            print(e)

    def SubmitEdit(self):
        try:
            test_electricity = float(self.lineEdit_Electricity.text()) if self.lineEdit_Electricity.text() else self.edit_date['test_electricity']

            test_mode = float(self.lineEdit_Mode.text()) if self.lineEdit_Mode.text() else self.edit_date['test_mode']

            resistor_ao = float(self.lineEdit_A.text()) if self.lineEdit_A.text() else self.edit_date['resistor_ao']

            resistor_bo = float(self.lineEdit_B.text()) if self.lineEdit_B.text() else self.edit_date['resistor_bo']

            resistor_co = float(self.lineEdit_C.text()) if self.lineEdit_C.text() else self.edit_date['resistor_co']

            data = {
                'test_electricity': test_electricity,
                'test_mode': test_mode,
                'resistor_ao': resistor_ao,
                'resistor_bo': resistor_bo,
                'resistor_co': resistor_co,
            }
            self.parent.Signal_Edit.emit(data)
            self.close()
        except Exception as e:
            print('修改数据返回ERROR：', e)


# 回路电阻测试仪
class LR_EditWindow(QWidget, LR_Form):
    Signal_Get_Date = pyqtSignal(object)

    def __init__(self, parent=None):
        super(LR_EditWindow, self).__init__()

        self.parent = parent

        self.setupUi(self)

        self.Signal_Get_Date.connect(self.ShowData)

        self.pushButton.clicked.connect(self.SubmitEdit)

        self.edit_date = None

    def ShowData(self, data):
        try:
            self.lineEdit_Current.setText(str(data['current']))
            self.lineEdit_Resistor.setText(str(data['resistor']))
            self.lineEdit_Voltage.setText(str(data['voltage']))
            self.edit_date = data
        except Exception as e:
            print(e)

    def SubmitEdit(self):
        try:
            current = float(self.lineEdit_Current.text()) if self.lineEdit_Current.text() else self.edit_date['current']

            resistor = float(self.lineEdit_Resistor.text()) if self.lineEdit_Resistor.text() else self.edit_date['resistor']

            voltage = float(self.lineEdit_Voltage.text()) if self.lineEdit_Voltage.text() else self.edit_date['voltage']

            data = {
                'current': current,
                'resistor': resistor,
                'voltage': voltage
            }
            self.parent.Signal_Edit.emit(data)
            self.close()
        except Exception as e:
            print('修改数据返回ERROR：', e)


# 接地导通仪
class EC_EditWindow(QWidget, EC_Form):
    Signal_Get_Date = pyqtSignal(object)

    def __init__(self, parent=None):
        super(EC_EditWindow, self).__init__()

        self.parent = parent

        self.setupUi(self)

        self.Signal_Get_Date.connect(self.ShowData)

        self.pushButton.clicked.connect(self.SubmitEdit)

        self.edit_date = None

    def ShowData(self, data):
        try:
            # print(data)
            self.lineEdit_Current.setText(str(data['current']))
            self.lineEdit_Resistor.setText(str(data['resistor']))
            self.edit_date = data
        except Exception as e:
            print('接收数据错误：', e)

    def SubmitEdit(self):
        try:
            current = float(self.lineEdit_Current.text()) if self.lineEdit_Current.text() else self.edit_date['current']

            resistor = float(self.lineEdit_Resistor.text()) if self.lineEdit_Resistor.text() else self.edit_date['resistor']

            data = {
                'current': current,
                'resistor': resistor
            }
            # print(data)
            self.parent.Signal_Edit.emit(data)
            self.close()
        except Exception as e:
            print('接地导通修改数据返回ERROR：', e)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = EC_EditWindow()
    mainWindow.show()
    sys.exit(app.exec_())
